﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Comm.DBHelper;
using System.Reflection;
using Comm.PublicClass;
using System.Windows.Forms;
using Comm.MultiLan;

namespace WsProtocol.IEC62056MDL
{
    [Serializable]
    public class HHUProMeterNumber
    {
        private static string ptltable = "T_HHUProMeterNumber";
        public static List<HHUProMeterNumber> AllHHUProMeterlst = new List<HHUProMeterNumber>();  //HHU抄读数据总表

        public string MeterNumber { get; set; }  //表号
        public string ExecutionTime { get; set; } //操作时间
        public string ProGramName { get; set; }   //方案名
        public int Seq { get; set; }

        public List<HHUProMeterNumber> SchLst //方案列表
        {
            get { return GetProMeterByNo(this.MeterNumber); }
        }

        public List<HHULog> HHULoglst;  //数据详情


        public HHUProMeterNumber()
        {
            MeterNumber = "";
            ExecutionTime = "";
            ProGramName = "";
            HHULoglst = new List<HHULog>();
        }

        public static void StartTrans()
        {
            PtlConsts.easyDb.BeginTrans();
        }

        public static void RollbackTrans()
        {
            PtlConsts.easyDb.RollbackTrans();
        }

        public static void CommitTrans()
        {
            PtlConsts.easyDb.CommitTrans();
        }


        private static List<HHUProMeterNumber> TableToList(DataTable dt)
        {
            List<HHUProMeterNumber> list = new List<HHUProMeterNumber>();
            foreach (DataRow dr in dt.Rows)
            {
                HHUProMeterNumber m = GetModelByDataRow(dr);
                
                list.Add(m);
            }
            return list;
        }


        /// <summary>
        /// 数据行转对象
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        public static HHUProMeterNumber GetModelByDataRow(DataRow dr)
        {
            HHUProMeterNumber m = new HHUProMeterNumber();
            m.MeterNumber = dr["MeterNumber"].ToString();
            m.ExecutionTime = dr["ExecutionTime"].ToString();
            m.ProGramName = dr["ProGramName"].ToString();
            m.Seq = MyConvert.getInt32(dr["Seq"].ToString());

            m.HHULoglst = HHULog.GetHHULogByMeter(m.MeterNumber, m.ExecutionTime);
            return m;
        }
       

        /// <summary>
        /// 获取所有表对象
        /// </summary>
        /// <returns></returns>
        public static List<HHUProMeterNumber> GetAllProMeter()
        {            
            PtlConsts.easyDb.Clear();
            PtlConsts.easyDb.TableName = ptltable;
            PtlConsts.easyDb.SetOrder("Seq");
            AllHHUProMeterlst = TableToList(PtlConsts.easyDb.Select());
            return AllHHUProMeterlst;
        }


        /// <summary>
        /// 获取表对象字典
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, HHUProMeterNumber> GetProMeterDic()
        {
            Dictionary<string, HHUProMeterNumber> dic = new Dictionary<string, HHUProMeterNumber>();
            if (AllHHUProMeterlst.Count == 0)
                GetAllProMeter();
            for (int i = 0; i < AllHHUProMeterlst.Count; i++)
            {
                if (!dic.ContainsKey(AllHHUProMeterlst[i].MeterNumber))
                    dic.Add(AllHHUProMeterlst[i].MeterNumber, AllHHUProMeterlst[i]);
            }
            return dic;
        }
        

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <param name="errinfo"></param>
        /// <returns></returns>
        public static int DeleteProMeter(string meterno, string atime, ref string errinfo)
        {
            try
            {
                PtlConsts.easyDb.Clear();
                PtlConsts.easyDb.TableName = ptltable;
                if (!string.IsNullOrEmpty(meterno))
                    PtlConsts.easyDb.SetWhereParameter(true, "MeterNumber", "=", meterno, EasyDataType.String);
                if (!string.IsNullOrEmpty(atime))
                    PtlConsts.easyDb.SetWhereParameter(true, "ExecutionTime", "=", atime, EasyDataType.String);
                int rs = PtlConsts.easyDb.Delete(ref errinfo);
                if (rs < 0)
                    return rs;
                rs = HHULog.DelHHULogByMeter(meterno, atime, ref errinfo);

                return rs;
            }
            catch(Exception ex)
            {
                errinfo = ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// 根据表号获取表对象
        /// </summary>
        /// <param name="meterno"></param>
        /// <returns></returns>
        public static List<HHUProMeterNumber> GetProMeterByNo(string meterno)
        {
            if (AllHHUProMeterlst.Count == 0)
                GetAllProMeter();

            List<HHUProMeterNumber> alst = new List<HHUProMeterNumber>();
            for (int i = 0; i < AllHHUProMeterlst.Count; i++)
            {
                if (AllHHUProMeterlst[i].MeterNumber == meterno)
                    alst.Add(AllHHUProMeterlst[i]);
            }

            return alst;
        }

    }
}
